<template>
    <div class="about">
        <navBar :titleName="titleName"></navBar>
        <tabbar :message="msg"></tabbar>
        <!--<nut-textinput class="up"-->
        <!--:clearBtn = "true"-->
        <!--label="就诊人"-->
        <!--:hasBorder="false"-->
        <!--placeholder="选择就诊人"-->
        <!--v-model="val1"-->
        <!--/>-->
        <mycell
                title="就 诊 人"
                :desc="patientName"
                :showIcon="true"
                :isLink="true"
                @myClickHandler="choosePatient">
        </mycell>
        <!--<nut-textinput-->
        <!--:clearBtn = "true"-->
        <!--label="预约时间"-->
        <!--:hasBorder="false"-->
        <!--placeholder="选择日期"-->
        <!--v-model="val"-->
        <!--/>-->
        <mycell v-if="this.$store.state.adviceType !== 1"
                title="就诊日期"
                :desc="adviceTime"
                :showIcon="true"
                :isLink="true"
                @myClickHandler="myClickDate()">
        </mycell>

        <!--<nut-actionsheet :is-visible="isVisible"
                         @close="switchActionSheet('isVisible')"
                         :menu-items="menuItems"
                         @choose="chooseItem"
        ></nut-actionsheet>-->

        <nut-datepicker
                :is-visible="isVisible"
                title="请选择日期时间"
                type="datetime"
                :startDate="startDate"
                :endDate="endDate"
                :defaultValue="today"
                @close="switchPicker('isVisible')"
                @choose="chooseItem"
        >
        </nut-datepicker>

        <nut-button @click="addMyPatient" block style="height: 48px;">
            + 新增病人
        </nut-button>

        <nut-button @click="addByPatient" block style="height: 48px;">
            + 邀请病人
        </nut-button>

        <nut-button @click="adviceFunc" block shape="circle" style="background-color: #4552ea;border-radius: 5px;width: 95%;
    height: 48px;font-size: 17px;margin-top: 40px;">
            确&nbsp;&nbsp;&nbsp;定
        </nut-button>

        <nut-dialog :visible="dialogShow" title="邀请病人扫码注册" @close="dialogShow=false" :noFooter="noFooter">
            <logoQrcode :content="link" :width="width"/>
        </nut-dialog>

    </div>

</template>

<script>
    /* eslint-disable no-console*/
    import mycell from '@/components/common/my-cell.vue'
    import tabbar from '@/components/common/tabbar.vue'
    import navBar from '@/components/common/navBar.vue'

    import logoQrcode from '@/components/common/my-code.vue'

    export default {
        name: 'advice',
        components: {
            tabbar,
            navBar,
            mycell,
            logoQrcode
        },
        beforeRouteLeave(to, from, next) {
            //进入新增病人页才缓存
            if (!this.addPatient) {
                this.patientName = '请选择';
                this.adviceTime = '请选择';
                this.$store.state.adviceType = 0;
            }
            this.addPatient = false;
            // 设置下一个路由的 meta
            to.meta.keepAlive = true;  // 让 A 缓存，即不刷新
            next();
        },
        data() {
            return {
                id: "advice",

                msg: 1,
                titleName: '约诊',

                patientName: '请选择',
                adviceTime: '请选择',
                adviceNoon: "",
                patientId: "",
                today: "2018-11-02 11:08",
                startDate: "2018-11-02",
                endDate: "2018-11-02",


                noFooter: true,
                dialogShow: false,
                link: "http://api.51self-service.cn/wx/authorize?returnUrl=http%3A%2F%2Fp.51self-service.cn?type=addPtient&did="
                + this.$store.state.doctor.doctorId,
                width: 175,

                bgColor: '#fff',
                fgColor: '#000',
                value: 'http://51self-service.cn/images/qrcode.png',
                // logo: "http://51self-service.cn/images/qrcode.png",

                isVisible: false,
                val: "",
                val1: "",
                menuItems: [],
                flag: 0,
                addPatient: false,
                adviceType: 0
            }
        },
        created() {
            this.isVisible = false;

            let d = new Date();
            this.today = d.Format("yyyy-MM-dd hh:mm");
            this.startDate = d.Format("yyyy-MM-dd");
            d.setMonth(d.getMonth() + 1);
            this.endDate = d.Format("yyyy-MM-dd");
            if (this.$store.state.adviceType === 1) {
                this.adviceTime = d.Format("yyyy-MM-dd hh:mm");
                this.adviceNoon = d.Format("hh") > 12 ? "pm" : "am"
            }
        },
        updated() {
            if (this.$store.state.adviceType === 1) {
                let d = new Date();
                this.adviceTime = d.Format("yyyy-MM-dd hh:mm");
                this.adviceNoon = d.Format("hh") > 12 ? "pm" : "am"
            }
        },
        computed: {
            patient() {
                return this.$store.state.patient
            },
            type() {
                return this.$store.state.adviceType
            }
        },
        watch: {
            patient(curVal, oldVal) {
                //这里的curVal就是需要监听的值
                console.log("--------watch patient---------------");
                console.log(curVal);
                console.log(oldVal);

                if (curVal && curVal.patientName) {
                    this.patientName = curVal.patientName;
                    this.patientId = curVal.patientId;
                }
                console.log("--------watch patient---------------");
            },
            type(curVal, oldVal) {
                //这里的curVal就是需要监听的值
                console.log("--------watch adviceType---------------");
                console.log(curVal);
                console.log(oldVal);

                this.adviceType = this.$store.state.adviceType;
                console.log("--------watch adviceType---------------");
            }
        },
        methods: {
            addMyPatient: function () {
                console.log("添加就诊人页面！！！");
                this.addPatient = true;
                this.$router.push({
                    name: 'addPatient'
                });
            },
            addByPatient: function () {
                this.link = "http://api.51self-service.cn/wx/authorize?returnUrl=http%3A%2F%2Fp.51self-service.cn?did="
                    + this.$store.state.doctor.doctorId;
                this.dialogShow = true;
            },
            switchPicker(param) {
                this[`${param}`] = !this[`${param}`];
            },
            chooseItem(itemParams) {
                console.log("------------------about chooseItem------------------");
                console.log(itemParams);
                this.adviceTime = itemParams[5];
                if (itemParams[3] > 11) {
                    this.adviceNoon = "pm";
                } else {
                    this.adviceNoon = "am";
                }
                console.log("------------------about chooseItem------------------");
            },
            choosePatient: function () {
                this.addPatient = true;
                this.$router.push({name: 'searchPatient'});
            },
            myClickDate: function () {
                this.isVisible = true;
            },
            adviceFunc: function () {
                if (!this.adviceTime) {
                    this.$toast.text("请选择约诊时间");
                    return;
                }
                if (!this.patientId) {
                    this.$toast.text("请选择约诊病人");
                    return;
                }
                if (!this.$store.state.doctor || !this.$store.state.doctor.doctorId) {
                    this.$toast.text("请重新登录");
                    window.location.href = "/";
                    return;
                }
                this.API.POST(this.ApiUrl.POST_ADD_ORDER, {
                    "adviceDate": this.adviceTime,
                    "adviceNoon": this.adviceNoon,
                    "patientId": this.patientId,
                    "doctorId ": this.$store.state.doctor.doctorId
                }).then(({data}) => {
                    console.log("约诊记录------", data);
                    if (data.code === 1200) {
                        if (this.$store.state.adviceType === 1) {
                            //快速接诊，约诊后进入接诊页面
                            /*this.$router.push({
                                name: 'acceptInfo', params: data.data
                            });*/
                            /*if (this.$store.state.accept) {
                                console.log("---------destroy accept--------");
                                this.$store.state.accept.$destroy();
                                console.log("---------destroy accept--------");
                            }*/
                            this.$store.state.medicineList = [];
                            this.$store.state.freeInfoList = [];
                            this.$store.state.diagMain = null;
                            this.$store.state.diagSecond = null;
                            let _that = this;
                            setTimeout(function () {
                                console.log("---------push accept--------");
                                _that.$router.push({
                                    name: 'acceptInfo', params: data.data
                                });
                            },50)

                        } else {
                            this.$toast.text("约诊成功");
                        }
                    }
                }).catch(err => {
                    console.log("addOrder::err------", err);
                });
            }
        }
    }
</script>
<style>
    .about {
        background-color: #e6e6e6;
        margin-top: 41px;
        width: 100%;
        min-height: 626px;
    }

    .nut-textinput input {
        -webkit-box-flex: 1;
        -webkit-flex: 1;
        flex: 1;
        height: 40px;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        font-size: 14px;
        box-sizing: border-box;
        border-radius: 0px !important;
        border: 1px solid #DDDFE6;
        text-align: end !important;
        background-color: '#FFFFFF';
        padding: 0 30px 0 10px;
    }

    .nut-textinput {
        display: -webkit-box;
        display: -webkit-flex;
        display: flex;
        -webkit-box-align: center;
        -webkit-align-items: center;
        align-items: center;
        position: relative;
        background-color: white !important;
        padding-left: 15px;
        border-bottom: 0.5px solid #DDDFE6;
    }

    .nut-textinput-label {
        width: 65px !important;
        text-align: -webkit-auto;
    }

    .nut-button.block {
        width: 100%;
        border-radius: 0;
        font-size: 16px;
        margin-top: 20px;
        background: white;
        color: #cccccc;
    }

    .nut-button circle block {
        background-color: #4552ea;
        border-radius: 5px;
        width: 95%;
        height: 48px;
        font-size: 17px;
        margin-top: 40px;
    }

    .nut-dialog-body {
        padding: 10px;
    }
</style>
